package com.eastfair.venuebooking.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.base.BaseControllerInfo;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.echo.core.EchoService;
import com.eastfair.venuebooking.demand.PriceListSpaceGroup;
import com.eastfair.venuebooking.dto.PriceListSpacePageQuery;
import com.eastfair.venuebooking.dto.PriceListSpaceQuery;
import com.eastfair.venuebooking.dto.PriceListSpaceSaveDTO;
import com.eastfair.venuebooking.dto.PriceListSpaceUpdateDTO;
import com.eastfair.venuebooking.entity.PriceListSpace;
import com.eastfair.venuebooking.service.PriceListSpaceService;
import com.eastfair.venuebooking.vo.PriceListSpaceVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 价目表场地关联
 * </p>
 *
 * @author linan
 * @date 2022-06-08
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/priceListSpace")
@Api(value = "PriceListSpace", tags = "价目表场地关联")
@PreAuth(replace = "venuebooking:priceListSpace:")
public class PriceListSpaceController extends SuperController<PriceListSpaceService, Long, PriceListSpace, PriceListSpacePageQuery, PriceListSpaceSaveDTO, PriceListSpaceUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private PriceListSpaceService priceListSpaceService;

    @Override
    public void handlerResult(IPage<PriceListSpace> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<PriceListSpace> priceListSpaceList = list.stream().map((map) -> {
            PriceListSpace priceListSpace = PriceListSpace.builder().build();
            //TODO 请在这里完成转换
            return priceListSpace;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(priceListSpaceList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<PriceListSpacePageQuery> params) {
        PriceListSpacePageQuery pageQuery = params.getModel();
        //分页处理
        if(ContextUtil.getProjectId()!=null){
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 查询价目场地列表.
     *
     * @param query the query
     * @return the r
     */
    @PostMapping("/query/priceListSpace")
    @BaseControllerInfo(voGroup = {PriceListSpaceGroup.QueryPriceListSpace.class})
    public R<List<PriceListSpaceVO>> queryPriceListSpace(@RequestBody PriceListSpaceQuery query){
        log.info("queryPriceListSpace - 查询价目场地列表, query={}", query);
        return R.successDef(priceListSpaceService.listPriceListSpaces(query));
    }

    /**
     * 查询价目下的场地标签.
     *
     * @param priceListIds the price list ids
     * @return the r
     */
    @PostMapping("/tags")
    public R<List<String>> queryTags(@RequestBody @NotEmpty(message = "价目ID列表不可为空") List<Long> priceListIds) {
        log.info("queryTags - 查询价目下的场地标签, priceListIds={}", priceListIds);
        return R.successDef(priceListSpaceService.listTags(priceListIds));
    }
}
